package com.situ.yB2C.product.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.situ.yB2C.product.pojo.Product;
import com.situ.yB2C.product.service.ProductService;
import com.situ.yB2C.user.controller.UserController;

@Controller
@RequestMapping("product/")
public class ProductController {
	private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private ProductService productService;
	
	
	@RequestMapping("index")
	public ModelAndView goIndex(ModelAndView modelAndView) {
		modelAndView.setViewName("product/index");
		return modelAndView;
	}
	
	//进form
	
	@RequestMapping("goform")
	public ModelAndView goForm(ModelAndView modelAndView) {
		modelAndView.setViewName("product/form");
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping("doadd")
	public int doAddProduct(Product product) {
		
		return productService.saveProduct(product);
	}
//	@ResponseBody
//	@RequestMapping("list")
//	public ModelAndView findAllProductList(ModelAndView modelAndView){
//		List<Product> productList = productService.findAllProduct();
//		modelAndView.addObject("productList", productList);
//		modelAndView.setViewName("product/list");
//		return modelAndView;
//	}
	
	@RequestMapping("list")
	public List<Product> findAllProductList(){
		return productService.findAllProduct();
	}
	@ResponseBody
	@RequestMapping("dodelete")
	public int deleteProduct (Long rowId) {
		return productService.deleteProductById(rowId);
	}
	
	@ResponseBody
	@RequestMapping("goedit")
	public ModelAndView goEdit(Long rowId,ModelAndView modelAndView) {
		Product product = productService.findOneById(rowId);
		modelAndView.addObject("product", product);
		modelAndView.setViewName("product/list");
		return modelAndView;
		
	}

	//执行修改
	@ResponseBody
	@RequestMapping("doedit")
	public int doEdit(Product product) {
		return productService.updateByProductInfo(product);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
